package cube.utils.comp;

import cube.utils.BeanUtil;

/**
 * 对象equals比较器
 * 
 * @author abiao
 */
public class EqualsComparor {

    /**
     * 需要比较的两个对象
     */
    private Object o;
    private Object rhs;

    /**
     * 对象equals比较器
     */
    public EqualsComparor(Object o, Object rhs) {
        this.o = o;
        this.rhs = rhs;
    }

    /**
     * 针对两个对象的属性进行比较
     */
    public boolean equals(String... props) {
        if (o == null || rhs == null) {
            return false;
        }
        for (int i = 0; i < props.length; i++) {
            if (!equals(o, rhs, props[i])) {
                return false;
            }
        }
        return true;
    }

    /**
     * 比较两个对象的属性
     */
    private boolean equals(Object o1, Object o2, String prop) {
        Object p1 = get(o, prop);
        Object p2 = get(rhs, prop);
        return (p1 == null && p2 == null) || (p1 != null && p1.equals(p2));
    }

    /**
     * 获取属性
     */
    private Object get(Object o, String prop) {
        return BeanUtil.readValue(o, prop);
    }
}
